using System;
using System.Collections.Generic;
using System.Text;
using PH.Common;

namespace Hoshimi
{
    public class ShooterBot_MoveToTargetState : State
	{
		#region Singleton

		// Fields
		protected static ShooterBot_MoveToTargetState instance;

		// Constructor
		protected ShooterBot_MoveToTargetState() : base() { }

		// Methods
		public static ShooterBot_MoveToTargetState Instance()
		{
			// Uses "Lazy initialization"
			if( instance == null ) 
				instance = new ShooterBot_MoveToTargetState();

			return instance;
		}

		#endregion

		public override bool Play( Unit unit ) 
        {
			ShooterBot bot = (ShooterBot) unit;

            bot.SetName("MoveToTarget");

            // We cant give order to the bot. We stop
			if ( bot.State != NanoBotState.WaitingOrders && bot.State != NanoBotState.Moving )
			{
				return true;
			}

            if (bot.UpdateTargetLocation())
            {
                bot.StopIfMoving();
            }

            if (bot.UpdateEnnemyTarget())
            {
                bot.StopIfMoving();
            }

            if(bot.IsEnnemyTargetReached())
            {
                unit.myStateMachine.ChangeState(ShooterBot_AttackTargetState.Instance());
                return false;
            }
            
            // We can't give orders to the bot. We stop
			if ( bot.State == NanoBotState.Moving) 
			{
				return true;
			}

            if (!bot.HasTargetLocation())
            {
                unit.myStateMachine.ChangeState(ShooterBot_RestState.Instance());
                return true;
            }

			if (bot.IsTargetLocationReached())
			{
				bot.AttackNearestEnnemyDirection();
				return true;
			}
			
            bot.MoveToTarget();

            return true;
		}
	}
}
